#include "AI2.h"


 int AI2::AnalyzeMove(Checkers &yourTurn)
{
	Board *move = yourTurn.GetBoard();

	int score = move->GetPlayer1PieceCount() - move->GetPlayer2PieceCount();

	return score;
}

	
int AI2::Alpha_Beta(bool maxNode, int alpha, int beta, int currentDepth, Checkers &yourTurn, Board currentState)
{
	if (currentDepth == difficulty_level) // if at the bottom of the tree
		return AnalyzeMove(yourTurn); // evaluate move

	if (yourTurn.GetStatus() == STATUS_PLAYER2_WON) // if this move means player 2 wins
		return ALPHA; // this is the worst move that can be made

	if (yourTurn.GetStatus() == STATUS_PLAYER1_WON) // if this move means player 1 wins
		return BETA; // this is the best move
	
	vector<MoveData*> moves; // an empty vector for the moves
	int maxMoves = yourTurn.GetLegalMoves(moves); // gets the legal moves for this turn 
	int score; // the value of this node
	
	Board *PnextMove;
	Board nextMove; // the board after this move is made

	if (maxNode)
	{

		for (int i = 0; i < maxMoves; i++)
		{
			// Try the move
			yourTurn.Move(moves[i]->StartX(),moves[i]->StartY(),moves[i]->EndX(),moves[i]->EndY());
			PnextMove = yourTurn.GetBoard();
			nextMove = *PnextMove;	
			yourTurn.ChangePlayerTurn(); // change the player turn
			score = Alpha_Beta(false,alpha,beta,currentDepth+1,yourTurn,nextMove); // move deeper down the tree

			if(score > alpha) // if the score is less than alpha
				alpha = score; // score becomes new alpha

			if(alpha >= beta) // if alpha is less than or equal to beta
				return alpha;  // cut off

			 yourTurn.SetBoard(currentState); //reset the game board
			 yourTurn.ChangePlayerTurn(); // reset the turn

		}
		return alpha;
	}

	else
	{
		for (int i = 0; i < maxMoves; i++)
		{
			// Try the move
			yourTurn.Move(moves[i]->StartX(),moves[i]->StartY(),moves[i]->EndX(),moves[i]->EndY());
			PnextMove = yourTurn.GetBoard();
			nextMove = *PnextMove;
			yourTurn.ChangePlayerTurn(); // change the player turn
			score = Alpha_Beta(true,alpha,beta,currentDepth+1,yourTurn,nextMove); // move deeper down the tree

			if(score > alpha) // if the score is less than alpha
				beta = score; // score becomes new alpha

			if(alpha >= beta) // if alpha is less than or equal to beta
				return beta;  // cut off

			 yourTurn.SetBoard(currentState); //reset the game board
			 yourTurn.ChangePlayerTurn(); // reset the turn

		}
	return beta;
	}
}

void AI2::FinalMove(Checkers &yourTurn, Board currentState)
{
	
	vector<MoveData*> moves; // an empty vector for the moves
	int maxMoves = yourTurn.GetLegalMoves(moves); // gets the legal moves for this turn 
	int score; // the value of this node
	int bestScore = ALPHA;
	int bestMove = 0;

	Board *PnextMove;
	Board nextMove; // the board after this move is made

	for (int i = 0; i < maxMoves; i++)
	{
		// Try the move
		yourTurn.Move(moves[i]->StartX(),moves[i]->StartY(),moves[i]->EndX(),moves[i]->EndY());

		PnextMove = yourTurn.GetBoard();
		nextMove = *PnextMove;

		if (yourTurn.GetStatus() == STATUS_PLAYER2_WON) // if this move means player 2 wins
		{
			yourTurn.SetBoard(currentState); //reset the game board
			yourTurn.ChangePlayerTurn(); // reset the turn
		}

		if (yourTurn.GetStatus() == STATUS_PLAYER1_WON) // if this move means player 1 wins
		{
			bestMove = i;
			yourTurn.SetBoard(currentState); //reset the game board
			yourTurn.ChangePlayerTurn(); // reset the turn
			break;
		}
				
		yourTurn.ChangePlayerTurn(); // change the player turn
		score = Alpha_Beta(false,ALPHA,BETA,0,yourTurn,nextMove); //gets the score for this move
		if (score > bestScore)
		{
			bestScore = score;
			bestMove = i;
		}
		yourTurn.SetBoard(currentState); //reset the game board
		yourTurn.ChangePlayerTurn(); // reset the turn

	}


	// execute the best move
	yourTurn.Move(moves[bestMove]->StartX(),
				  moves[bestMove]->StartY(),
				  moves[bestMove]->EndX(),
				  moves[bestMove]->EndY());
}